package com.rtx.common;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

import com.alibaba.druid.sql.visitor.functions.Char;
import com.alibaba.fastjson.JSONObject;
import com.rtx.api.annotation.RtxRequestIgnore;
import com.rtx.edi.model.express.bo.ExpressBO;

@SuppressWarnings("all")
public class ClassDescJsonStr {
	
	public static void getSTDRequest(String calssName,Field curField,JSONObject root,Set<Class<?>> set) {
		try {
			getSTDRequest(Class.forName(calssName),curField,root,set);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	public static JSONObject getSTDRequest(Class calzz,Field curField,JSONObject root,Set<Class<?>> set) {
		if(calzz == null || calzz.getDeclaredFields().length <1) {
			return root;
		}
		
		if(curField!=null) {
			//忽略的属性
			RtxRequestIgnore ignore =	(RtxRequestIgnore) curField.getDeclaredAnnotation(RtxRequestIgnore.class);
			if(ignore != null) {
				return root;
			}
		}

		
		
		if(null == curField) {

			for(Field field:calzz.getDeclaredFields()) {
				getSTDRequest(field.getClass(),field,root,set);
			}
		}else {
			Class  fieldClassType =curField.getType();
			
			if(Arrays.asList(String.class,Long.class,Integer.class,Double.class,Float.class,Short.class,Boolean.class,Char.class,Byte.class).contains(fieldClassType)) {
				root.put(curField.getName(), "");
				return root;
			} else if(fieldClassType.equals(List.class)) {
				ArrayList list = new ArrayList();
				JSONObject child = new JSONObject();
				Class cclazz =	getFieldTypeList(curField);
				getSTDRequest(cclazz,null,child,set);
				list.add(child);
				root.put(curField.getName(), list);
				return root;
			}else if(set.contains(fieldClassType)) {
				JSONObject child = new JSONObject();
				getSTDRequest(fieldClassType,null,child,set);
				root.put(curField.getName(), child);
				return root;
			}else {
				return root;
			}
		}
		
		return root;
	}  
	
	public static Class getFieldTypeList(Field f) {
		Type  genericType  = f.getGenericType();
		ParameterizedType pt =(ParameterizedType) genericType;
		Class<?> actualTypeArgument = (Class<?>)pt.getActualTypeArguments()[0];
		return actualTypeArgument;
	}
	
}
